--这个文件不适合直接复制（需要修改的内容很多） 可以看看如何实现的然后再复制
--需要注意 这个组件需要加在双端 也就是主客通用部分
--AddItemTrigger是用来加对应的动作函数的 需要写在物品的fn里面

--写在动作测试里面添加动作和对应的获取动作
do
    local actions = {
        {
            id = 'ACTION_HERTA_GIVE_ITEM',
            str = STRINGS.INFO_HERTA.ACTIONS.ACTION_HERTA_GIVE_ITEM,
            fn = function(act)
                local target = act.target
                local herta_item_receiver = target and target.components.herta_item_receiver
                if herta_item_receiver and herta_item_receiver:ReceiveItem(act.invobject, act.doer) then
                    return true
                else
                    return false
                end
            end,
            state = 'give',
            actiondata = {
                priority = 99,
            },
        },
    }
    local component_actions = {
        {
            type = 'USEITEM',
            component = 'inventoryitem',
            tests = {
                {
                    action = 'ACTION_HERTA_GIVE_ITEM',
                    testfn = function(inst, doer, target, actions, right)
                        if right and doer:HasTag('player')
                            and target:HasTag("herta_item_receiver") then
                            local herta_item_receiver = target.components.herta_item_receiver
                            return herta_item_receiver:CanReceiveItem_client(inst)
                        end
                    end,
                },
            },
        },
    }
end
--写在组件里面
local herta_item_receiver = Class(function(self, inst)
    inst:AddTag("herta_item_receiver")
    self.inst = inst
    self.receiverd_prefab = {}
    self.receiverd_num = {}
    self.receiverd_fn = {}
end)
function herta_item_receiver:CopyDataFromOtherComponents(othercomponent)
    --receiverd_num
    for k, v in pairs(othercomponent.receiverd_num) do
        if self.receiverd_num[k] and v ~= 0 then
            self.receiverd_num[k] = v
            if self.receiverd_fn[k] then
                for i = 1, v do
                    self.receiverd_fn[k](self.inst, nil, i, k)
                end
            end
        end
    end
end

function herta_item_receiver:AddItemTrigger(prefab, fn, maxnum, per_require_num)
    self.receiverd_fn[prefab] = fn
    self.receiverd_prefab[prefab] = { maxnum = maxnum, per_require_num = per_require_num }
    self.receiverd_num[prefab] = 0
end

function herta_item_receiver:CanReceiveItem_client(item)
    local prefab = item.prefab
    if self.receiverd_prefab[prefab]
    then
        return true
    end
end

function herta_item_receiver:CanReceiveItem(item)
    local prefab = item.prefab
    if self.receiverd_prefab[prefab]
        and (self.receiverd_num[prefab] < self.receiverd_prefab[prefab].maxnum or self.receiverd_prefab[prefab].maxnum == -1)
        and (not self.receiverd_prefab[prefab].per_require_num
            or item.components.stackable and item.components.stackable:StackSize() >= self.receiverd_prefab[prefab].per_require_num)
    then
        return true
    end
end

function herta_item_receiver:ConsumeReceiveItem(item)
    local prefab = item.prefab
    local per_require_num = self.receiverd_prefab[prefab].per_require_num
    if item.components.stackable then
        if per_require_num then
            item.components.stackable:Get(per_require_num):Remove()
        else
            item.components.stackable:Get():Remove()
        end
    else
        item:Remove()
    end
end

function herta_item_receiver:ReceiveItem(item, doer)
    local prefab = item.prefab
    if self:CanReceiveItem(item)
    then
        self.receiverd_num[prefab] = (self.receiverd_num[prefab] or 0) + 1
        if self.receiverd_fn[prefab] then
            self.receiverd_fn[prefab](self.inst, item, self.receiverd_num[prefab], prefab, doer)
        end
        self:ConsumeReceiveItem(item)
        return true
    end
end

function herta_item_receiver:OnSave()
    if not self.ban_save then
        return {
            receiverd_num = self.receiverd_num,
        }
    end
end

function herta_item_receiver:OnLoad(data)
    if not self.ban_save then
        if data.receiverd_num ~= nil then
            for k, num in pairs(data.receiverd_num) do
                if self.receiverd_num[k] then
                    self.receiverd_num[k] = num
                end
                if self.receiverd_fn[k] and num ~= 0 then
                    for i = 1, num do
                        self.receiverd_fn[k](self.inst, nil, i, k)
                    end
                end
            end
        end
    end
end

return herta_item_receiver



--[[
local function ongetitem(inst, item, nownum, prefab)
    if prefab == string.lower("VOIDCLOTH_KIT") then

    end
end
inst:AddComponent("muu_item_receiver")
    inst.components.muu_item_receiver:AddItemTrigger(string.lower("VOIDCLOTH_KIT"), ongetitem, -1) ]]
